#include "uartcom.h"

/* USART init function uart*/

void (*uart1_callback)( uint8_t *buff, uint32_t len );
void (*uart2_callback)( uint8_t *buff, uint32_t len );
void (*uart3_callback)( uint8_t *buff, uint32_t len );

void (*uart4_callback)( uint8_t *buff, uint32_t len  );
void (*uart5_callback)( uint8_t *buff, uint32_t len );
void (*uart6_callback)( uint8_t *buff, uint32_t len );

uint8_t uart1_EC20_rxbuf[huart1_EC20_DMARxdata_MAX];
uint8_t uart1_EC20_txbuf[huart1_EC20_DMATxdata_MAX];


uint8_t uart2_screen_rxbuf[huart2_screen_DMARxdata_MAX];
uint8_t uart2_screen_txbuf[huart2_screen_DMATxdata_MAX];


uint8_t uart3_XYencoder_rxbuf[huart3_XYencoder_DMARxdata_MAX];
uint8_t uart3_XYencoder_txbuf[huart3_XYencoder_DMATxdata_MAX];

uint8_t uart4_rxbuf[huart4_DMARxdata_MAX];
uint8_t uart4_BJencoder_txbuf[huart4_BJencoder_DMATxdata_MAX];

uint8_t uart5_motor_driver_rxbuf[huart5_motor_driver_DMARxdata_MAX];
uint8_t uart5_motor_driver_txbuf[huart5_motor_driver_DMATxdata_MAX];

uint8_t uart6_rxbuf[huart6_DMARxdata_MAX];
uint8_t uart6_txbuf[huart6_DMATxdata_MAX];

void uart1_init( void *callback )
{
	uart1_callback = ( void (*)(uint8_t *, uint32_t ) )callback;

	__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);

	HAL_UART_Receive_DMA(&huart1, uart1_EC20_rxbuf, sizeof(uart1_EC20_rxbuf));
//        HAL_UARTEx_ReceiveToIdle_DMA(&huart1,uart1_EC20_rxbuf,sizeof(uart1_EC20_rxbuf));
//        HAL_UART_Receive_IT(&huart1, uart1_EC20_rxbuf, sizeof(uart1_EC20_rxbuf));
}

void uart1_clearrxbuf( void )
{
  memset(uart1_EC20_rxbuf, 0, huart1_EC20_DMARxdata_MAX);
}

void uart1_recv( void )
{
	unsigned int 	temp			= 0;
	unsigned int 	tmp_flag 	        = 0;
	unsigned int	rxlen			= 0;

	tmp_flag = __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE);

	if((tmp_flag != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart1);
		temp = huart1.Instance->SR;
		temp = huart1.Instance->DR;
		HAL_UART_DMAStop(&huart1);
		temp  = hdma_usart1_rx.Instance->NDTR ;
		rxlen =  huart1_EC20_DMARxdata_MAX - temp;
//                if(rxlen != 8) return;
		if( (rxlen > 0) && (rxlen < huart1_EC20_DMARxdata_MAX) )
		{
                        if( ( uart1_callback != NULL ) && ( rxlen <= huart1_EC20_DMARxdata_MAX ))
			{
				uart1_callback( uart1_EC20_rxbuf, rxlen );
			}
		}
		uart1_clearrxbuf();
		HAL_UART_Receive_DMA(&huart1, uart1_EC20_rxbuf, sizeof(uart1_EC20_rxbuf));
	 }

	tmp_flag =__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC);
	if((tmp_flag != RESET))
	{
// 		RS481_CTRL_RECV();
//		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_TC);

	}
}
void uart2_init( void *callback )
{
	uart2_callback = ( void (*)(uint8_t *, uint32_t ) )callback;

	__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);

	HAL_UART_Receive_DMA(&huart2, uart2_screen_rxbuf, sizeof(uart2_screen_rxbuf));
//        HAL_UARTEx_ReceiveToIdle_DMA(&huart2,uart2_screen_rxbuf,sizeof(uart2_screen_rxbuf));
//        HAL_UART_Receive_IT(&huart2, uart2_screen_rxbuf, sizeof(uart2_screen_rxbuf));
}

void uart2_clearrxbuf( void )
{
  memset(uart2_screen_rxbuf, 0, huart2_screen_DMARxdata_MAX);
}

void uart2_recv( void )
{
	unsigned int 	temp			= 0;
	unsigned int 	tmp_flag 	        = 0;
	unsigned int	rxlen			= 0;

	tmp_flag = __HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE);
	if((tmp_flag != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart2);
		temp = huart2.Instance->SR;
		temp = huart2.Instance->DR;
		HAL_UART_DMAStop(&huart2);
		temp  = hdma_usart2_rx.Instance->NDTR ;
		rxlen =  huart2_screen_DMARxdata_MAX - temp;
		if( (rxlen > 0) && (rxlen < huart2_screen_DMARxdata_MAX) )
		{
                        if( ( uart2_callback != NULL ) && ( rxlen <= huart2_screen_DMARxdata_MAX ))
			{
				uart2_callback( uart2_screen_rxbuf, rxlen );
			}
		}
		uart2_clearrxbuf();
		HAL_UART_Receive_DMA(&huart2, uart2_screen_rxbuf, sizeof(uart2_screen_rxbuf));
	 }

	tmp_flag =__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TC);
	if((tmp_flag != RESET))
	{
// 		RS485_CTRL_RECV();
//		__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_TC);

	}
}
void uart3_init( void *callback )
{
	uart3_callback = ( void (*)(uint8_t *, uint32_t ) )callback;

	__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);

	HAL_UART_Receive_DMA(&huart3, uart3_XYencoder_rxbuf, sizeof(uart3_XYencoder_rxbuf));
//        HAL_UARTEx_ReceiveToIdle_DMA(&huart3,uart3_EC20_rxbuf,sizeof(uart3_EC20_rxbuf));
//        HAL_UART_Receive_IT(&huart3, uart3_EC20_rxbuf, sizeof(uart3_EC20_rxbuf));
}

void uart3_clearrxbuf( void )
{
  memset(uart3_XYencoder_rxbuf, 0, huart3_XYencoder_DMARxdata_MAX);
}

void uart3_recv( void )
{
	unsigned int 	temp			= 0;
	unsigned int 	tmp_flag 	        = 0;
	unsigned int	rxlen			= 0;

	tmp_flag = __HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE);
	if((tmp_flag != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart3);
		temp = huart3.Instance->SR;
		temp = huart3.Instance->DR;
		HAL_UART_DMAStop(&huart3);
		temp  = hdma_usart3_rx.Instance->NDTR ;
		rxlen =  huart3_XYencoder_DMARxdata_MAX - temp;
		if( (rxlen > 0) && (rxlen < huart3_XYencoder_DMARxdata_MAX) )
		{
                        if( ( uart3_callback != NULL ) && ( rxlen <= huart3_XYencoder_DMARxdata_MAX ))
			{
				uart3_callback( uart3_XYencoder_rxbuf, rxlen );
			}
		}
		uart3_clearrxbuf();
		HAL_UART_Receive_DMA(&huart3, uart3_XYencoder_rxbuf, sizeof(uart3_XYencoder_rxbuf));
	 }

	tmp_flag =__HAL_UART_GET_FLAG(&huart3, UART_FLAG_TC);
	if((tmp_flag != RESET))
	{
// 		RS485_CTRL_RECV();
//		__HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_TC);

	}
}
void uart4_init( void *callback )
{
	uart4_callback = (void (*)(uint8_t *, uint32_t ))callback;

	__HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);

	HAL_UART_Receive_DMA(&huart4, uart4_rxbuf, sizeof(uart4_rxbuf));
}

void uart4_clearrxbuf( void )
{
  memset(uart4_rxbuf, 0, huart4_DMARxdata_MAX);
}

void uart4_recv( void )
{
	unsigned int 	temp			= 0;
	unsigned int 	tmp_flag 	        = 0;
	unsigned int	rxlen			= 0;

	tmp_flag =__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE);
	if((tmp_flag != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart4);
		temp = huart4.Instance->SR;
		temp = huart4.Instance->DR;
		HAL_UART_DMAStop(&huart4);
		temp  = hdma_uart4_rx.Instance->NDTR ;
		rxlen =  huart4_DMARxdata_MAX - temp;
		if( (rxlen > 1) && (rxlen < huart4_DMARxdata_MAX) )
		{
                        if( ( uart4_callback != NULL ) && ( rxlen <= huart4_DMARxdata_MAX ))
			{
 				uart4_callback( uart4_rxbuf, rxlen );
			}
		}
		uart4_clearrxbuf();
		HAL_UART_Receive_DMA(&huart4, uart4_rxbuf, sizeof(uart4_rxbuf));
	 }

	tmp_flag =__HAL_UART_GET_FLAG(&huart4, UART_FLAG_TC);
	if((tmp_flag != RESET))
	{
// 		RS485_CTRL_RECV();
//		__HAL_UART_CLEAR_FLAG(&huart4, UART_FLAG_TC);

	}
}

void uart5_init( void *callback )
{
	uart5_callback = ( void (*)(uint8_t *, uint32_t ) )callback;

	__HAL_UART_ENABLE_IT(&huart5, UART_IT_IDLE);

	HAL_UART_Receive_DMA(&huart5, uart5_motor_driver_rxbuf, sizeof(uart5_motor_driver_rxbuf));
//        HAL_UARTEx_ReceiveToIdle_DMA(&huart5,uart5_motor_driver_rxbuf,sizeof(uart5_motor_driver_rxbuf));
}

void uart5_clearrxbuf( void )
{
  memset(uart5_motor_driver_rxbuf, 0, huart5_motor_driver_DMARxdata_MAX);
}

void uart5_recv( void )
{
	unsigned int 	temp			= 0;
	unsigned int 	tmp_flag 	        = 0;
	unsigned int	rxlen			= 0;

	tmp_flag = __HAL_UART_GET_FLAG(&huart5, UART_FLAG_IDLE);
	if((tmp_flag != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart5);
		temp = huart5.Instance->SR;
		temp = huart5.Instance->DR;
		HAL_UART_DMAStop(&huart5);
		temp  = hdma_uart5_rx.Instance->NDTR ;
		rxlen =  huart5_motor_driver_DMARxdata_MAX - temp;
		if( (rxlen > 0) && (rxlen < huart5_motor_driver_DMARxdata_MAX) )
		{
                        if( ( uart5_callback != NULL ) && ( rxlen <= huart5_motor_driver_DMARxdata_MAX ))
			{
				uart5_callback( uart5_motor_driver_rxbuf, rxlen );
			}
		}
		uart5_clearrxbuf();
		HAL_UART_Receive_DMA(&huart5, uart5_motor_driver_rxbuf, sizeof(uart5_motor_driver_rxbuf));
	 }

	tmp_flag =__HAL_UART_GET_FLAG(&huart5, UART_FLAG_TC);
	if((tmp_flag != RESET))
	{
// 		RS485_CTRL_RECV();
//		__HAL_UART_CLEAR_FLAG(&huart5, UART_FLAG_TC);

	}
}

void uart6_init( void *callback )
{
	uart6_callback = ( void (*)(uint8_t *, uint32_t ) )callback;

	__HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE);

	HAL_UART_Receive_DMA(&huart6, uart6_rxbuf, sizeof(uart6_rxbuf));
//        HAL_UARTEx_ReceiveToIdle_DMA(&huart6,uart6_motor_driver_rxbuf,sizeof(uart6_motor_driver_rxbuf));
}

void uart6_clearrxbuf( void )
{
  memset(uart6_rxbuf, 0, huart6_DMARxdata_MAX);
}

void uart6_recv( void )
{
	unsigned int 	temp			= 0;
	unsigned int 	tmp_flag 	        = 0;
	unsigned int	rxlen			= 0;

	tmp_flag = __HAL_UART_GET_FLAG(&huart6, UART_FLAG_IDLE);
	if((tmp_flag != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart6);
		temp = huart6.Instance->SR;
		temp = huart6.Instance->DR;
		HAL_UART_DMAStop(&huart6);
		temp  = hdma_usart6_rx.Instance->NDTR ;
		rxlen =  huart6_DMARxdata_MAX - temp;
		if( (rxlen > 0) && (rxlen < huart6_DMARxdata_MAX) )
		{
                        if( ( uart6_callback != NULL ) && ( rxlen <= huart6_DMARxdata_MAX ))
			{
				uart6_callback( uart6_rxbuf, rxlen );
			}
		}
		uart6_clearrxbuf();
		HAL_UART_Receive_DMA(&huart6, uart6_rxbuf, sizeof(uart6_rxbuf));
	 }

	tmp_flag =__HAL_UART_GET_FLAG(&huart6, UART_FLAG_TC);
	if((tmp_flag != RESET))
	{
// 		RS486_CTRL_RECV();
//		__HAL_UART_CLEAR_FLAG(&huart6, UART_FLAG_TC);

	}
}
